package laba4;

import interfaces.task4.MapUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author yakimovich
 */
public class MapUtilsImpl implements MapUtils {

    /**
     *Build a frequency dictionary for triplets of letters in the text
     * @param string string to search for triples of letters
     * @return Map of founded triples of letters and the number of its occurrences in the specified String
     */
    @Override
    public Map<String, Integer> findThrees(String string) {
        Map<String, Integer> result = new HashMap<>();
        Matcher m = Pattern.compile("[a-zA-Z0-9]{3}").matcher(string);

        int i = 0;
        int count = 1;
        while (m.find(i)) {
            String triple = m.group();
            if (result.containsKey(triple)) {
                count = result.get(triple);
                count++;
            }
            result.put(triple, count);
            count = 1;
            i = m.start() + 1;
        }
        return result;
    }
}
